package test07;

import java.util.*;

/**
 * 处理器链管理类
 */
public class HandlerChain {
    private ContentHandler chainHead;
    private List<ContentHandler> handlers;
    private Map<ContentType, List<String>> chainConfigs;

    public HandlerChain() {
        this.handlers = new ArrayList<>();
        this.chainConfigs = new HashMap<>();
        initializeDefaultConfigs();
    }

    private void initializeDefaultConfigs() {
        // 文本内容审核链
        chainConfigs.put(ContentType.TEXT, Arrays.asList(
                "敏感词检测器", "版权检测器", "质量评估器", "幻觉内容检测器"
        ));

        // 图像内容审核链
        chainConfigs.put(ContentType.IMAGE, Arrays.asList(
                "深度伪造检测器", "版权检测器", "质量评估器"
        ));

        // 视频内容审核链
        chainConfigs.put(ContentType.VIDEO, Arrays.asList(
                "深度伪造检测器", "版权检测器", "敏感词检测器"
        ));

        // 教育类简化流程
        chainConfigs.put(ContentType.AUDIO, Arrays.asList(
                "深度伪造检测器", "版权检测器"
        ));
    }

    /**
     * 根据内容类型构建处理链
     */
    public void buildChain(ContentType contentType) {
        System.out.println("🔗 构建" + contentType + "内容审核链...");

        List<String> handlerNames = chainConfigs.get(contentType);
        if (handlerNames == null || handlerNames.isEmpty()) {
            System.out.println("⚠️ 未找到该内容类型的配置，使用默认文本审核链");
            handlerNames = chainConfigs.get(ContentType.TEXT);
        }

        ContentHandler previous = null;
        for (String handlerName : handlerNames) {
            ContentHandler handler = findHandlerByName(handlerName);
            if (handler != null) {
                if (chainHead == null) {
                    chainHead = handler;
                }
                if (previous != null) {
                    previous.setNext(handler);
                }
                previous = handler;
                System.out.println("   ✅ 添加处理器: " + handlerName);
            }
        }

        if (chainHead != null) {
            System.out.println("✅ " + contentType + "审核链构建完成");
        }
    }

    private ContentHandler findHandlerByName(String handlerName) {
        for (ContentHandler handler : handlers) {
            if (handler.handlerName.equals(handlerName)) {
                return handler;
            }
        }
        System.out.println("⚠️ 未找到处理器: " + handlerName);
        return null;
    }

    /**
     * 处理审核请求
     */
    public void processRequest(ContentCheckRequest request) {
        System.out.println("\n🎯 开始处理审核请求: " + request);

        if (chainHead == null) {
            buildChain(request.getContentType());
        }

        if (chainHead != null) {
            chainHead.handleCheck(request);
        } else {
            System.out.println("❌ 审核链未正确构建");
        }

        // 显示处理历史
        System.out.println("\n📋 处理历史:");
        for (ProcessingRecord record : request.getProcessingHistory()) {
            System.out.println("   " + record);
        }
    }

    public void addHandler(ContentHandler handler) {
        handlers.add(handler);
        System.out.println("注册处理器: " + handler.handlerName);
    }

    public void removeHandler(String handlerName) {
        handlers.removeIf(handler -> handler.handlerName.equals(handlerName));
        System.out.println("移除处理器: " + handlerName);
    }

    public List<String> getChainConfig(ContentType contentType) {
        return chainConfigs.get(contentType);
    }

    public void setChainConfig(ContentType contentType, List<String> handlerNames) {
        chainConfigs.put(contentType, handlerNames);
        System.out.println("更新" + contentType + "审核链配置: " + handlerNames);
    }
}


